package com.ts.api.module.distribute.entity.bo;

import com.ts.api.module.distribute.entity.DistributeLinkPO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

/**
 *  分配链接存储redis实体
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class DistributeLinkBO {

    /** 子链接主键 */
    private Long id;
    /** 链接组主键 */
    private Long groupId;
    /** 链接切换数 */
    private Integer linkMaxClick;
    /** 子链接地址 */
    private String url;
    /** 当前点击数 */
    private Integer count;
    /** 子链接状态 0禁用1启用 */
    private Integer status;

    public void increment() {
        this.count++;
    }

    /**
     * 创建链接BO对象
     * @param list
     * @return
     */
    public static List<DistributeLinkBO> create(List<DistributeLinkPO> list) {
        List<DistributeLinkBO> listBO = new ArrayList<>();

        for (DistributeLinkPO distributePO: list) {
            listBO.add(new DistributeLinkBO(
                distributePO.getId(),
                distributePO.getGroupId(),
                distributePO.getLinkMaxClick(),
                distributePO.getUrl(),
                0,
                distributePO.getStatus()
                )
            );
        }
        return listBO;
    }

    /**
     * 转换PO实体
     * @param list
     * @return
     */
    public static List<DistributeLinkPO> convertPO(List<DistributeLinkBO> list) {
        List<DistributeLinkPO> listPO = new ArrayList<>();

        for (DistributeLinkBO distributePO: list) {
            listPO.add(new DistributeLinkPO(
                    distributePO.getId(),
                    distributePO.getGroupId(),
                    distributePO.getLinkMaxClick(),
                    distributePO.getUrl(),
                    distributePO.getStatus()
                )
            );
        }
        return listPO;
    }

    /**
     * 获取首个元素并自增
     * <br>获取首个非禁用的链接</>
     * @param list
     * @return
     */
    public static DistributeLinkBO firstElementIncrement(List<DistributeLinkBO> list) {
        DistributeLinkBO result = null;
        for (DistributeLinkBO distributeBO: list) {
            Integer enableStatus = 1;
            if (enableStatus.equals(distributeBO.getStatus())) {
                distributeBO.increment();
                result = distributeBO;
                break;
            }
        }
        return result;
    }

    /***
     * 将BO与PO进行值对比
     *
     * @param distributeLinkPO
     * @return
     */
    public boolean equalsPO(DistributeLinkPO distributeLinkPO) {

        return true;
    }
}
